package com.ys.controller.api.business;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.config.MenuId;
import com.ys.entity.business.TpAmazonShop;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.business.TpAmazonShopService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService.UploadDto;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/tpAmazonShop")
public class ApiInterTpAmazonShopController {
	
	@Autowired
	private CommonApiService<TpAmazonShop> commonApiService;
	
	@Autowired
	private TpAmazonShopService tpAmazonShopService;
	
	private static Long MENU_ID_WD = 10194L;//关联网店商品

	private final Long MENU_ID_FBA = MenuId.TCC_FbaPlan;//FBA计划单
	private final Long MENU_ID_FBA1 = MenuId.TCC_FbaPlanX;//FBA计划单
	private final Long MENU_ID_CA = MenuId.THR_OperateCompanyApply;//运营公司

	@RequiresPermissions("tpAmazonShop:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(@ModelAttribute TpAmazonShop cond){
		
		cond = setSeachSql(cond);
		return commonApiService.searchLay(cond);
	}
	
	/**
	 * 新增
	 */
	@RequestMapping("/add")
	@ResponseBody
	@RequiresPermissions("tpAmazonShop:add")
	public R add(Long amazonSellerId, String subData) {
		try {
			//修改
			return tpAmazonShopService.add(amazonSellerId, subData);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}

	/**
	 * 编辑
	 */
	@RequiresPermissions("tpAmazonShop:edit")
	@PostMapping("/edit")
    @ResponseBody
	public R edit(Long amazonSellerId, String subData){
		try {
			return tpAmazonShopService.edit(amazonSellerId, subData);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}
	
	/*
	 * 导出
	 */
	@RequiresPermissions("tpAmazonShop:export")
	@RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TpAmazonShop cond) throws Exception{
		
		cond = setSeachSql(cond);
		cond.setOrderby("A.Id DESC");
		
		cond.setPage(0).setSize(999999);

		List<Map<String, Object>> listData = cond.queryMap();
        
    	List<YsCol> listField = new YsCol()
    			.setMenuId(MenuId.TB_Site)
    			.setFrontShow(YsColEnum.FrontShow.SHOW.value)
    			.setOrderby("A.SortNo asc")
    			.queryList();
    	
    	
        ExportExcelUtils<TpAmazonShop> util = new ExportExcelUtils<TpAmazonShop>(new TpAmazonShop());
        
        String toFilePath = util.exportExcel(listData, listField, "亚马逊店铺管理");
        UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }

	/**
	 * 弹出框
	 */
	@RequestMapping("/searchForModal")
	@ResponseBody
	public LayuiTableVoJA searchForModal(@ModelAttribute TpAmazonShop cond){
		cond.setStatus(1);
		return commonApiService.searchLay(cond);
	}

    /**
     * 设置查询的cond的sql设置
     */
    public TpAmazonShop setSeachSql(TpAmazonShop cond){
    	cond = commonApiService.dataAuth(MenuId.TP_AmazonShop, cond);
    	
    	cond.addLeftJoin("TP_AmazonSeller B ON B.id = A.AmazonSellerId")
    		.addField("B.Vgsbm vgsbm")
    		.addField("B.Vgsjc vgsjc");
    	
    	cond.addLeftJoin("THR_OperateCompanyApply B1 ON B1.Vgsbm = B.Vgsbm")
    		.addLeftJoin("TS_UserChange B2 ON B2.Id = B1.OperatorChangeId")
    		.addField("B2.UserId operatorUserId")
    		.addField("B2.UserName operatorUserName");
    	
    	cond.addLeftJoin("TB_Site C ON C.Id = A.SiteId")
    		.addLeftJoin("TP_AmazonRegion D ON D.Code = C.RegionCode")
    		.addField("D.Code regionCode")
    		.addField("D.Name regionName")
    		
    		.addLeftJoin("TB_Platform E ON E.Code = D.PlatformCode")
    		.addField("E.Code platformCode")
    		.addField("E.Name platformName")
    		
    		.addLeftJoin("TB_Country F ON F.Id = C.CountryId")
    		.addField("F.Code countryCode")
    		.addField("F.Name countryName");
    		
    		
    	
//    	cond.addLeftJoin("TP_AmazonRegion B ON B.Code = A.RegionCode")
//    		.addField("B.Name regionName");
//    	
//    	cond.addLeftJoin("TB_Platform B1 ON B1.Code = B.PlatformCode")
//    		.addField("B1.Code platformCode")
//    		.addField("B1.Name platformName");
    	
        return  cond;
    }


	@RequestMapping("/searchTck")
	@ResponseBody
	public LayuiTableVoJA searchTck(@ModelAttribute TpAmazonShop cond){

		cond.setOrderby("A.CreateTime desc");
		cond.addLeftJoin("TP_AmazonSeller tsell on tsell.id = A.amazonSellerId");//卖家管理
		cond.addField("tsell.vgsbm companyID");
		return commonApiService.searchLay(cond);
	}

	@RequestMapping("/searchGl")
	@ResponseBody
	public LayuiTableVoJA searchGl(@ModelAttribute TpAmazonShop cond){
		cond.addLeftJoin("TP_AmazonSeller tsell on tsell.id = A.amazonSellerId");//卖家管理
		cond.addField("tsell.regionName");
		cond.addLeftJoin("THR_OperateCompanyApply THRO ON  THRO.VGsBm = tsell.Vgsbm");
		cond.addLeftJoin("TB_Site ts on ts.id = A.SiteId");//站点
		cond.addField("ts.AmazonMarketplaceId AS marketplaceIds");
		cond.addLeftJoin("TB_Country tc on tc.id = ts.CountryId");//国家
		cond.addField("tc.name AS countryName");
		cond.addField("THRO.CUser AS cUser,tsell.Vgsbm companyID");
		cond.setOrderby("A.CreateTime desc");
		cond = commonApiService.dataAuth(MENU_ID_WD, cond);
		return commonApiService.searchLay(cond);
	}

	/**
	 * 查询只有大站点的店铺
	 * @param model
	 * @param request
	 * @param response
	 * @param cond
	 * @return
	 */
	@RequestMapping("/searchDp")
	@ResponseBody
	public LayuiTableVoJA searchDp(@ModelAttribute TpAmazonShop cond){
		cond.addLeftJoin("TB_Site ts on ts.id = A.SiteId");//站点
		cond.addLeftJoin("TB_Country tc on tc.id = ts.CountryId")//国家
				.addField("tc.name AS countryName,tc.code AS countryCode");
		cond.addLeftJoin("TP_AmazonSeller tsell on tsell.id = A.amazonSellerId");//卖家管理
		cond.addField("tsell.vgsbm companyID");
		cond.where("(tc.name = '英国' OR tc.name = '美国') AND tsell.authorizeState = '已授权'");
		cond.setOrderby("A.CreateTime desc");

		return commonApiService.searchLay(cond);
	}

	/**
	 * 万里汇多账户管理调用
	 */
	@RequestMapping("/searchWs")
	@ResponseBody
	public LayuiTableVoJA searchWs(@ModelAttribute TpAmazonShop cond){
		cond.addLeftJoin("THR_OperateCompanyApply THRO ON  THRO.VGsBm = A.Vgsbm");
		cond.addField("THRO.vgsBm AS vgsbm,THRO.vgsJc AS vgsjc");
		cond.setOrderby("A.CreateTime desc");

		return commonApiService.searchLay(cond);
	}

    /**
     * 获取亚马逊授权URL地址
     */
    @RequestMapping("/getAmazonAuthUrl")
    @ResponseBody
    public R getAmazonAuthUrl(Long shopId) {
        return tpAmazonShopService.getAmazonAuthUrl(shopId);
    }

    /**
     * 获取亚马逊广告授权URL地址
     */
    @RequestMapping("/getAmazonAuthUrlGg")
    @ResponseBody
    public R getAmazonAuthUrlGg(Long shopId) {
        return tpAmazonShopService.getAmazonAuthUrlGg(shopId);
    }


	/**
	 * Amazon店铺管理（HW采购计划调用）
	 */
	@RequestMapping("/searchHwJh")
	@ResponseBody
	public LayuiTableVoJA searchHwJh(@ModelAttribute TpAmazonShop cond){
		cond.addLeftJoin("TP_AmazonSeller tsell on tsell.id = A.amazonSellerId");//卖家管理
		cond.addField("tsell.vgsBm vgsBm,tsell.VgsJc vgsJc");
		cond.addLeftJoin("THR_OperateCompanyApply THRO ON  THRO.VGsBm = tsell.Vgsbm");
		cond.addField("THRO.vgsMc AS vgsMc");
		cond.addLeftJoin("TB_Site C ON C.Id = A.SiteId")

		.addLeftJoin("TB_Country F ON F.Id = C.CountryId")
		.addField("F.Code countryCode")
		.addField("F.Name countryName");
		cond.addLeftJoin("TP_AmazonShopExchangeRate C1 ON C1.Id = F.AmazonShopExchangeRateId")
		.addField("C1.Code currencyCode")
		.addField("C1.Name currencyName");
		cond.setOrderby("A.CreateTime desc");
		cond = commonApiService.dataAuth(MenuId.TOS_PurchasePlan, cond);


		return commonApiService.searchLay(cond);
	}

	@RequestMapping("/searchFba")
	@ResponseBody
	public LayuiTableVoJA searchFba(@ModelAttribute TpAmazonShop cond){
		cond.addLeftJoin("TP_AmazonSeller tsell on tsell.id = A.amazonSellerId");//卖家管理
		cond.addLeftJoin("THR_OperateCompanyApply B1 ON B1.Vgsbm = tsell.Vgsbm");
		cond .addLeftJoin("TS_UserChange U ON U.Id = B1.OperatorChangeId ");
		cond.addField("tsell.vgsJc,tsell.vgsbm");
		cond.setStatus(1);
		cond = commonApiService.dataAuth(MENU_ID_FBA1, cond);
		return commonApiService.searchLay(cond);
	}
}
